package com.icode.core.dto.house;

import com.icode.core.model.house.*;
import com.icode.core.shared.ImageUtils;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-3-2
 * Time: 下午2:03
 * To change this template use File | Settings | File Templates.
 */
public class HomePageDTO {
    private String id;
    private String guid;
    @NotEmpty(message = "请输入标题!")
    private String title;
    @NotEmpty(message = "请输入内容!")
    private String contents;
    private List<String> introImgGuids;
    private List<String> infoImgGuids;
    private List<String> partnersImgGuids;
    private List<MultipartFile> introImg;
    private List<MultipartFile> infoImg;
    private List<MultipartFile> partnersImg;

    public HomePageDTO() {
    }

    public HomePageDTO(String id, String guid, String title, String contents, List<String> introImgGuids, List<String> infoImgGuids, List<String> partnersImgGuids) {
        this.id = id;
        this.guid = guid;
        this.title = title;
        this.contents = contents;
        this.introImgGuids = introImgGuids;
        this.infoImgGuids = infoImgGuids;
        this.partnersImgGuids = partnersImgGuids;
    }

    public static HomePageDTO toDTO(HomePage homePage) {
        List<String> introImgGuids = initIntroImgGuids(homePage.introductionImg());
        List<String> infoImgGuids = initInfoImgGuids(homePage.infoImg());
        List<String> partnersImgGuids = initPartnerImgGuids(homePage.partners());
        return new HomePageDTO(homePage.id(), homePage.guid(), homePage.title(), homePage.contents(), introImgGuids, infoImgGuids, partnersImgGuids);
    }

    public HomePage toHomePage(HomePageDTO homePageDTO) {
        HomePage homePage = new HomePage(homePageDTO.getTitle(), homePageDTO.getContents());
        List<HomeIntroImage> introImgs = new ArrayList<>();
        List<HomeInfoImage> infoImgs = new ArrayList<>();
        List<HomePartnerImage> partnersImgs = new ArrayList<>();
        List<UploadImage> intros = ImageUtils.resolveUploadImages(homePageDTO.getIntroImg());
        List<UploadImage> infos = ImageUtils.resolveUploadImages(homePageDTO.getInfoImg());
        List<UploadImage> partners = ImageUtils.resolveUploadImages(homePageDTO.getPartnersImg());
        for (UploadImage u : intros) {
            introImgs.add(new HomeIntroImage(homePage, u));
        }
        for (UploadImage u : infos) {
            infoImgs.add(new HomeInfoImage(homePage, u));
        }
        for (UploadImage u : partners) {
            partnersImgs.add(new HomePartnerImage(homePage, u));
        }
        homePage.initImgs(introImgs, infoImgs, partnersImgs);
        if (!"".equals(homePageDTO.getId())) {
            homePage.setId(Integer.parseInt(homePageDTO.getId()));
        }
        return homePage;
    }

    public static List<String> initIntroImgGuids(List<HomeIntroImage> homePageImages) {
        List<String> guids = new ArrayList<>();
        for (HomeIntroImage h : homePageImages) {
            guids.add(h.image().guid());
        }
        return guids;
    }

    public static List<String> initInfoImgGuids(List<HomeInfoImage> homePageImages) {
        List<String> guids = new ArrayList<>();
        for (HomeInfoImage h : homePageImages) {
            guids.add(h.image().guid());
        }
        return guids;
    }

    public static List<String> initPartnerImgGuids(List<HomePartnerImage> homePageImages) {
        List<String> guids = new ArrayList<>();
        for (HomePartnerImage h : homePageImages) {
            guids.add(h.image().guid());
        }
        return guids;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setGuid(String guid) {
        this.guid = guid;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setContents(String contents) {
        this.contents = contents;
    }

    public void setIntroImgGuids(List<String> introImgGuids) {
        this.introImgGuids = introImgGuids;
    }

    public void setInfoImgGuids(List<String> infoImgGuids) {
        this.infoImgGuids = infoImgGuids;
    }

    public void setPartnersImgGuids(List<String> partnersImgGuids) {
        this.partnersImgGuids = partnersImgGuids;
    }

    public String getId() {

        return id;
    }

    public String getGuid() {
        return guid;
    }

    public String getTitle() {
        return title;
    }

    public String getContents() {
        return contents;
    }

    public List<String> getIntroImgGuids() {
        return introImgGuids;
    }

    public List<String> getInfoImgGuids() {
        return infoImgGuids;
    }

    public List<String> getPartnersImgGuids() {
        return partnersImgGuids;
    }

    public void setIntroImg(List<MultipartFile> introImg) {
        this.introImg = introImg;
    }

    public void setInfoImg(List<MultipartFile> infoImg) {
        this.infoImg = infoImg;
    }

    public void setPartnersImg(List<MultipartFile> partnersImg) {
        this.partnersImg = partnersImg;
    }

    public List<MultipartFile> getIntroImg() {

        return introImg;
    }

    public List<MultipartFile> getInfoImg() {
        return infoImg;
    }

    public List<MultipartFile> getPartnersImg() {
        return partnersImg;
    }
}
